//index.js
//获取应用实例
var app = new getApp();
var zUrl = getApp().globalData.zUrl;
Page({
  data: {
    canIUse: wx.canIUse
      ('button.open-type.getUserInfo')
  },
  //事件处理函数
  onLoad: function () {
  },

  /*
  *调起授权框
  */
  loginDialogGetUserInfo: function (res) {
    //把用户信息存储到全局变量中
    var globalData = app.globalData;
    globalData.wxUserBean = res.detail;
    globalData.userInfo = res.detail.userInfo;
    wx.getSetting({
      success:(res)=>{
        if (res.authSetting['scope.userInfo']) {
        wx.login({
          success: function (res) {
            wx.request({
              url: zUrl+'/api/login',
              data: {
                code: res.code
              },
              method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
              //预防乱码的不用管
              success: function (res) {
                wx.hideLoading()
                console.log(res)//处理返回的数据 
                var laravel_session = res.header["Set-Cookie"].match(/laravel_session=(.*?);/)[1]
                wx.setStorageSync('laravel_session', laravel_session)
                if (res.data.code == '200') {
                  if (!res.data.data) {
                    wx.redirectTo({
                      url: '/pages/register/index'
                    })
                  } else {
                    wx.setStorageSync('token', res.data.data.token)
                    wx.setStorageSync('adviser', res.data.data.adviser)
                    wx.switchTab({
                      url: '/pages/index/index'
                    })
                  }
                } else {
                  wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                  })
                }

              },
              fail: function (error) {

              },
              complete: function (res) {

              }
            });
          }
        })
        }else{
          wx.showToast({
            title: '抱歉！你拒绝了授权无法使用该微信小程序',
            icon: 'none',
            duration: 3000
          })
        }
      }
    })
  },
})